package com.anytron.config;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;

import com.anytron.model.res.NameSpaceContent;

public class ApoloConfigureCache {

	public static final Map<String, NameSpaceContent> APOLLO_CACHE_MAP = new ConcurrentHashMap<>();

	public static void setCache(NameSpaceContent nsc) {
		APOLLO_CACHE_MAP.put(nsc.getPin(), nsc);
	}

	public static NameSpaceContent getCache(String pin) {
		return APOLLO_CACHE_MAP.get(pin);
	}

	public static boolean isNeedUpdate(NameSpaceContent nsc) {
		String newContent = nsc.getContent();
		if (StringUtils.isBlank(newContent)) {
			return false;
		}
		NameSpaceContent oldCache = getCache(nsc.getPin());
		if (oldCache != null) {
			if (nsc.getPath().equals(oldCache.getPath()) && oldCache.getContent().equals(newContent)) {
				return false;
			}
		}
		return true;
	}

}
